import store from "@/store"
import Vue from 'vue'
Vue.filter('count', function(count) {
    if (!count) {
        return 0
    }
    var arr = []
    while (true) {
        var temp = count / 1000
        if (temp < 1) {
            arr.unshift(count)
            break
        } else {
            var more = count % 1000
            arr.unshift(more)
            count = Math.floor(temp)
        }
    }
    return arr.join(',')
})
Vue.filter('imgUrl', function(value) {
    if (!value) {
        return ''
    }
    value = value.replace(/\\/g, '/')
    value = value.replace('http:', 'https:')
    if (/^https:/.test(value)) {
        return value
    } else {
        return 'https://' + value
    }
})


Vue.filter("offsetTime", function(date) {

    var str = ''
    var now = new Date().getTime()
    date = date.replace(/-/g, "/")
    date = date.replace(/T/g, " ")
    date = new Date(date).getTime()
    var offset = now - date
    offset = Math.floor(offset / 1000)
    if (offset < 3600) {
        str = Math.ceil(offset / 60) + '分钟前'
        return str
    }

    if (offset < 3600 * 24) {
        str = Math.floor(offset / 3600) + '小时前'
        return str
    }

    if (offset < 3600 * 24 * 30) {
        str = Math.floor(offset / 3600 / 24) + '天前'
        return str
    }

    if (offset < 3600 * 24 * 30 * 12) {
        str = Math.floor(offset / 3600 / 24 / 30) + '月前'
        return str
    }

    str = Math.floor(offset / 3600 / 24 / 30 / 12) + '年前'
    return str
})
Vue.filter("chatTime", function(date) {
    date= date.replace(/-/g,"/")
    date= date.replace(/T/g," ")
    var theDay = date
    var str = ''
    var now = new Date().getTime()
    var nowDate = new Date()
    var theDate = new Date(date)
    var nowYear = nowDate.getFullYear()
    var theYear = theDate.getFullYear()
    var nowMonth = nowDate.getMonth()
    var theMonth = theDate.getMonth()
    var nowDay = nowDate.getDate()
    var oneDay = theDate.getDate()
    date = new Date(date).getTime()
    var hour = new Date(date).getHours()
    var minitus = new Date(date).getMinutes()
    if (hour < 10) hour = '0' + hour
    if (minitus < 10) minitus = '0' + minitus
    var offset = now - date
    offset = Math.floor(offset / 1000)
    if (nowYear == theYear && nowMonth == theMonth && nowDay == oneDay) {
        str = hour + ':' + minitus
        return str
    } else if (nowYear == theYear && nowMonth == theMonth && nowDay == oneDay + 1) {
        str = '昨天' + hour + ':' + minitus
        return str
    } else {
        str = theYear + '-' + (theMonth + 1) + '-' + oneDay + ' ' + hour + ':' + minitus
        return str
    }
})
Vue.filter("channelType", function(id) {

    var list = store.state.channelTypeList;

    var name = "";
    for (var item of list) {
        if (item.channelTypeId == id) {
            name = item.channelTypeName;
        }
    }
    if (name) {
        return name;
    } else {
        return "其它";
    }

});


Vue.filter("getDay", function(str) {
    var date = new Date(str);
    var weeks = new Array("星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六");
    var day = date.getDay();
    return weeks[day];
});


Vue.filter('formatDateTime', function(value, formatStr) {
    var self;
    if (value) {
        if (typeof value == 'string') {
            value = value.replace(/T/g, ' '); //去掉T
            value = value.replace(/-/g, '/');
            value = value.replace(/\.\d+/, ' '); //去掉毫秒
            self = new Date(value)
        } else {
            self = value
        }

    } else {
        self = new Date()

    }

    var str = formatStr
    var Week = ['日', '一', '二', '三', '四', '五', '六']
    str = str.replace(/yyyy|YYYY/, self.getFullYear())
    str = str.replace(/yy|YY/, (self.getYear() % 100) > 9 ? (self.getYear() % 100).toString() : '0' + (self.getYear() % 100))
    str = str.replace(/MM/, self.getMonth() > 8 ? (self.getMonth() + 1).toString() : '0' + (self.getMonth() + 1))
    str = str.replace(/M/g, self.getMonth())
    str = str.replace(/w|W/g, Week[self.getDay()])
    str = str.replace(/dd|DD/, self.getDate() > 9 ? self.getDate().toString() : '0' + self.getDate())
    str = str.replace(/d|D/g, self.getDate())
    str = str.replace(/hh|HH/, self.getHours() > 9 ? self.getHours().toString() : '0' + self.getHours())
    str = str.replace(/h|H/g, self.getHours())
    str = str.replace(/mm/, self.getMinutes() > 9 ? self.getMinutes().toString() : '0' + self.getMinutes())
    str = str.replace(/m/g, self.getMinutes())
    str = str.replace(/ss|SS/, self.getSeconds() > 9 ? self.getSeconds().toString() : '0' + self.getSeconds())
    str = str.replace(/s|S/g, self.getSeconds())
    return str
})

Vue.prototype.replaceUrl = function(str) {

    var reg = new RegExp('(https?|ftp|file)://[-A-Za-z0-9+&@#/%?=~_|!:,.;]+[-A-Za-z0-9+&@#/%=~_|]', 'g');


    var list = []

    var result = null
    do {
        result = reg.exec(str)
        result && list.push(result[0])
    } while (result)
    for (var i = 0; i < list.length; i++) {
        var item = list[i]
        str = str.replace(item, '<a href="' + item + '">' + item + '</a>')
    }
    return str

};

Vue.filter('menuImgUrl', function(value) {
    if (!value) {
        return ''
    }
    value = value.replace(/\\/g, '/')
    value = value.replace('http://apiV2-stucard.xueerqin.net/', ' https://srv.xueerqin.net/apiV2-stucard/')
    return value
})

// ——————————————————————————————————————————————————————————————————
Vue.filter('conversionOptions', function(e){
    switch(e){
      case 1: return "正确"; break;
      case 2: return "错误"; break;
      case 4: return "A"; break;
      case 8: return "B"; break;
      case 16: return "C"; break;
      case 32: return "D"; break;
      case 64: return "E"; break;
      case 128: return "F"; break;
      case 256: return "G"; break;
      case 512: return "H"; break;
      case 1025: return "I"; break;
      case 2048: return "J"; break;
      case 4096: return "K"; break;
      case 8192: return "L"; break;
      case 16384: return "M"; break;
      case 32768: return "N"; break;
      case true: return 1; break;
      case false: return 2; break;
      case "A": return 4; break;
      case "B": return 8; break;
      case "C": return 16; break;
      case "D": return 32; break;
      case "E": return 64; break;
      case "F": return 128; break;
      case "G": return 256; break;
      case "H": return 512; break;
      case "I": return 1025; break;
      case "J": return 2048; break;
      case "K": return 4096; break;
      case "L": return 8192; break;
      case "M": return 16384; break;
      case "N": return 32768; break;
    }
  })
  Vue.prototype.htmlDecode = function(str){
    if (str == null) return ''
      str = str.replace(/&amp;/g, "&");
      str = str.replace(/&#38;/g, "&");
      str = str.replace(/&#62;/g, ">");
      str = str.replace(/&gt;/g, ">");
      str = str.replace(/&#60;/g, "<");
      str = str.replace(/&lt;/g, "<");
      str = str.replace(/&nbsp;/g, " ");
      str = str.replace(/&#39;/g, "\'");
      str = str.replace(/&quot;/g, "\"");
      str = str.replace(/&#34;/g, "\"");
      str = str.replace(/&#x2F;/g, "\/");
      str = str.replace(/&amp;nbsp;/g, " ");
  
      // str = str.replace(/&nbsp;/g, " ");
      // console.log('str:',str)
      return str
  }